package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IStyleDAO;
import com.twentyOne.ClothingLand.vo.Style;

public class StyleDAO extends BaseDAO implements IStyleDAO {

	@Override
	public void addStyle(Style style) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(style);
		tx.commit();
		session.close();
		
	}

	@Override
	public void deleteStyle(Style style) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete Style as s where s.styleId = "+style.getStyleId()+"";
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public void modifyStyle(Style style) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Style set name=:name, description=:description where styleId=:styleId";
		Query query = session.createQuery(hql);
		query.setString("name", style.getName());
		query.setString("description", style.getDescription());
		query.setInteger("styleId", style.getStyleId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public List<Style> searchAllStyle() {
		
		Session session = getSession();
		String hql = "from Style";
		Query query = session.createQuery(hql);
		List<Style> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public Style searchByID(int styleId) {

		Session session = getSession();
		String hql = "from Style as s where s.styleId=" + styleId;
		Query query = session.createQuery(hql);
		List<Style> list = query.list();
		session.close();
		if(list.size() != 0) {
			Style style = list.get(0);
			return style;
		}else {
			return null;
		}		
		
	}

}
